#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
#define CLM 20
#define LINE 15
#define PRIME_SIZE 1000


//Hernan Leibovich
//ID = 324322601
//GROUP = 660001-4

/* function declaration */

void print_menu();
int RemoveDup(int[], int[]);
void print_array(int[], int);
void heart (char a[LINE][CLM]);
void fillshape(char[LINE][CLM],int,int);
void saddle_point(double [][SIZE],int);
void print_double_array(double [][SIZE], int );
double min_max(double [][11],int);
void fill_prime_array(char[],int);
void prime(char arr[],int );
void print_prime(char arr[],int );

/* main function */


int main(){
	int key, cont = 1;
	int arr1[SIZE] = {50,60,60,72,81,81,81,81,93,93};
	int arr2[SIZE];
	double arr3[SIZE][SIZE], minmax;
	int m=7,n=4, i=0, j=0;
	char arr2d[LINE][CLM];
	char prime_arr[1001];
	print_menu();
	while  (cont != 0){
		scanf("%d", &key);
		switch (key){
			case 1:{
				system("cls");
				printf("Before Removing duplicated values:\n");
				print_array(arr1, SIZE);
				RemoveDup(arr1, arr2, SIZE);
				print_menu();
				break;
				   }

			case 2:{
				system("cls");
				heart (arr2d);
				fillshape(arr2d,n,m);
				for (i=0;i<LINE;i++){
					for (j=0;j<CLM;j++){
						printf ("%c",arr2d[i][j]);
					}
					printf("\n");
				}
				print_menu();
				break;
				   }
			case 3:{
				system("cls");
				saddle_point(arr3, SIZE);
				print_double_array(arr3, SIZE);
				printf("\nThe biggest value in row and smallest value in column is:\n %4.2lf\n\n", min_max(arr3,SIZE));
				print_menu();
				break;
				   }
			case 4:{
				system("cls");
				fill_prime_array(prime_arr, PRIME_SIZE);
				prime(prime_arr, PRIME_SIZE);
				print_prime(prime_arr, PRIME_SIZE);
				print_menu();
				break;
				   }

			default:{
				system("cls");
				return 0;
					}
		}
	}
}

void print_menu(){
	printf("Welcome to Project 2 \n");
	printf("1- Remove Duplicated\n");
	printf("2- Shape Filler\n");
	printf("3- Saddle Point\n");
	printf("4- The Sieve of Eratosthenes\n");
	printf("0- Exit\n");
}

void print_array(int arr[], int size){
	int i;
	for( i = 0; i < size; i++){
		printf("%d, ", arr[i]);
	}
	printf("\n\n");
}

void print_double_array(double s_point[][SIZE], int size)
{
	int i,j;
	for(j=0;j<size;j++)
	{
		for(i=0;i<size;i++)
			printf("%3.1lf   ",s_point[j][i]);
		printf("\n\n");
	}
}

int RemoveDup(int arr1[], int arr2[], int size){
	int i, j, flag=0, counter = 0;
	for(i = 0; i < size; i++){
		for(j =0; j < size; j++){
			if (arr1[i] == arr2[j]){
				flag = 1; 
				break;
			}
		}
		if(flag ==1){
			flag = 0;
			continue;
		}
		arr2[counter] = arr1[i];
		counter++;
	}
	print_array(arr2, counter);
	return counter;
}

void fillshape (char arr[LINE][CLM],int m,int n)
{
	if (arr[m][n]!='*')
	{
		arr[m][n]='*';
		fillshape (arr,m+1,n);
		fillshape (arr,m-1,n);
		fillshape (arr,m,n+1);
		fillshape (arr,m,n-1);
	}
}

void heart (char a[LINE][CLM])
{
	int i=0,j=0;

	char str0[CLM] = "  ****       ****  ";
	char str1[CLM] = " *    *     *    * ";
	char str2[CLM] = "*      *   *      *";
	char str3[CLM] = "*       * *       *";
	char str4[CLM] = "*        *        *";
    char str5[CLM] = "*                 *";
	char str6[CLM] = " *               * ";
	char str7[CLM] = "  *             *  ";
	char str8[CLM] = "   *           *   ";
	char str9[CLM] = "    *         *    ";
	char stra[CLM] = "     *       *     ";
    char strb[CLM] = "      *     *      ";
	char strc[CLM] = "       *   *       ";
	char strd[CLM] = "        * *        ";
    char stre[CLM] = "         *         ";

	
	for (i=0;i<CLM;i++)		
	{
		a[0][i]=str0[i];
		a[1][i]=str1[i];
		a[2][i]=str2[i];
		a[3][i]=str3[i];
		a[4][i]=str4[i];
		a[5][i]=str5[i];
		a[6][i]=str6[i];
		a[7][i]=str7[i];
		a[8][i]=str8[i];
		a[9][i]=str9[i];
		a[10][i]=stra[i];
		a[11][i]=strb[i];
		a[12][i]=strc[i];
		a[13][i]=strd[i];
		a[14][i]=stre[i];
	}
	
	for (i=0;i<LINE;i++)
	{
		for (j=0;j<CLM;j++)
			printf ("%c",a[i][j]);
		printf("\n");
	}

}

void saddle_point(double s_point[][SIZE],int size){
	int x,y;
	double res , de_x , de_y;
	for (x=0; x < size + 1; x++){
		for(y=0; y < size + 1; y++){
			de_x = (x-4)*(x-4)/4.;
			de_y = (y-3)*(y-3)/9.;
			res = de_x - de_y + 5;
			s_point[x][y] = res;
		}
	}
}

double min_max(double s_point[][SIZE],int size){
	double res_x, res_y;
	int x,y,i,j,k;
    res_x = s_point[0][0];
    res_y = s_point[0][0];
    for(x=0; x < size; x++){
        i=x;
		for(y=0; y < size-1; y++){
			if(s_point[x][y] < s_point[x][y+1]){
                j = y+1;
                res_x = s_point[x][y+1];
                }
        }
        for(k=0; k < size-1; k++){
           if(s_point[k][j] > s_point[k+1][j]){
                res_y = s_point[k+1][j];
           }
        }
        if(res_x == res_y){
			return(res_x);
        }

    }
}

void fill_prime_array(char arr[],int s){
    int i;
    for(i=1;i<=s;i++)
      arr[i] = 't';
}

void prime(char arr[],int size){
    int i,j;
    for(j=3; j <= size; j++){
		for(i=2; i <= 32; i++){
			if((j % i == 0) && (j > i)){
				arr[j] = 'f';
			}
		}
    }
    arr[1]='f';
  }

void print_prime(char arr[],int size){
	int i;
	for(i=1;i<=size;i++){
		if(arr[i] == 't'){
			printf("%4d",i);
		}
	}
	printf("\n\n");
}